#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[]) {
    int pid;

    pid = fork();

    if (pid < 0) {
        fprintf(stderr, "Parent process fail to create child process\n");
        return -1;
    }

    if (pid > 0) {
        printf("Parent process create child process %d\n", pid);
        while(1);
    } else {
        setsid();

        /* Change the current working directory to the root. */
        if(chdir("/") < 0) {
            perror("child");
            exit(1);
        }

        /* Attach file descriptors 0 1 and 2 to /dev/null */
        close(0);
        open("/dev/null", O_RDWR);
        dup2(0, 1);
        dup2(0, 2);
    }

}